package com.braveman.im.ui.activity;

import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.braveman.im.R;
import com.braveman.im.ui.adapter.GroupMembersAdapter;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.ui.base.BaseActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCursorResult;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


public class GroupMembersActivity extends BaseActivity {

    private ListView mList;
    private GroupMembersAdapter groupAdapter;

    private String mChatId;
    private List<String> memberList = new ArrayList();
    ;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("群成员列表");
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mChatId = callIntent.getStringExtra("mChatId");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_group_im;
    }

    @Override
    protected void initChildView() {
        mList = (ListView) findViewById(R.id.list);

    }

    @Override
    protected void handleMessage(BaseActivity context, Message msg) {

    }

    @Override
    protected void initChildData() {
        groupAdapter = new GroupMembersAdapter(GroupMembersActivity.this, 1, memberList);

        refresh();

        mList.setAdapter(groupAdapter);
        if (groupAdapter != null)
            groupAdapter.notifyDataSetChanged();

    }

    @Override
    protected void setChildViewListener() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);


        switch (v.getId()) {


        }


    }

    private void refresh() {

        final int pageSize = 20;
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                EMCursorResult<String> result = null;
                do {
                    try {
                        result = EMClient.getInstance().groupManager().fetchGroupMembers(mChatId,
                                result != null ? result.getCursor() : "", pageSize);
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                    memberList.addAll(result.getData());

                }
                while (!TextUtils.isEmpty(result.getCursor()) && result.getData().size() == pageSize);
                EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_START_LOGIN));
            }
        });
        thread.start();

    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    public void onEventMainThread(EventCenter eventCenter) {
        switch (eventCenter.eventCode) {
            case EventCode.EVENT_CODE_START_LOGIN:
                if (groupAdapter != null)
                    groupAdapter.notifyDataSetChanged();
                break;
        }
    }

}
